package com.atung.parking.service.impl;

import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.controller.DateConvertUtil;
import com.atung.parking.entity.BusLicensePlate;
import com.atung.parking.entity.BusUser;
import com.atung.parking.mapper.BusUserMapper;
import com.atung.parking.request.query.QueryUserRequest;
import com.atung.parking.service.LicensePlateService;
import com.atung.parking.service.UserManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
public class UserManagerServiceImpl implements UserManagerService {
    @Resource
    private BusUserMapper busUserMapper;
    @Resource
    private LicensePlateService licensePlateService;

    @Override
    public PageInfoResponse<BusUser> queryUserListService(QueryUserRequest request) {

        QueryWrapper<BusUser> queryWrapper = this.createQueryWrapper(request);
        //车牌号
        if (StringUtils.hasLength(request.getLicenseNumber())) {
            List<BusLicensePlate> busLicensePlates = licensePlateService.queryLicenseNameService(request.getLicenseNumber());
            if (CollectionUtils.isEmpty(busLicensePlates)) {
                return new PageInfoResponse<>();
            }
            List<Long> userId = busLicensePlates.stream().map(item -> item.getUserId()).collect(Collectors.toList());
            queryWrapper.in("id", userId);

        }

        Page<BusUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<BusUser> busSuperUserPage = busUserMapper.selectPage(page, queryWrapper);

        PageInfoResponse<BusUser> busUserPageInfoResponse = new PageInfoResponse<>();
        busUserPageInfoResponse.setTotal(busSuperUserPage.getTotal());
        busUserPageInfoResponse.setData(busSuperUserPage.getRecords());
        return busUserPageInfoResponse;
    }

    public QueryWrapper<BusUser> createQueryWrapper(QueryUserRequest request) {
        QueryWrapper<BusUser> queryWrapper = new QueryWrapper<>();
        //
        if (Objects.nonNull(request.getUserId())) {
            queryWrapper.eq("id", request.getUserId());
        }
        //厂商
        if (StringUtils.hasText(request.getName())) {
            queryWrapper.eq("wx_name", request.getName());
        }
        //联系电话
        if (StringUtils.hasText(request.getPhone())) {
            queryWrapper.like("phone", request.getPhone());
        }
        if (StringUtils.hasText(request.getStartData()) && StringUtils.hasText(request.getEndData())) {
            Long start = DateConvertUtil.convertQueryStartToMillis(request.getStartData());
            Long end = DateConvertUtil.convertQueryEndToMillis(request.getEndData());
            if (Objects.nonNull(request.getStartData()) && Objects.nonNull(request.getEndData())) {
                queryWrapper.between("creator_time", start, end);
            }

        }
        queryWrapper.orderByDesc("creator_time");
        return queryWrapper;

    }
}
